import { Component, Input, OnInit } from '@angular/core';
import { FcsService } from "../../../../service/fcs.service";
import * as differenceInCalendarDays from 'date-fns/difference_in_calendar_days';
import { formatDate } from '@angular/common';
import { NzMessageService } from "ng-zorro-antd";

@Component({
  selector: 'app-function-log',
  templateUrl: './function-log.component.html',
  styleUrls: ['./function-log.component.less']
})
export class FunctionLogComponent implements OnInit {
  @Input() groupId = '';
  @Input() functionId = '';
  logList = [];
  pageSize = 10;
  pageNo = 1;
  totalCount = 0;
  isLogLoading = true;
  errorMsg = '';
  invokeTypeList = {
    "invoke": "控制台执行",
    "dev.knative.sources.ping": "定时触发器",
    "oss": '对象存储触发器',
    "apig": 'API 网关触发器'
  };
  requestId: string;
  startTime;
  endTime;
  status: string;
  dateFormat = 'yyyy-MM-dd HH:mm:ss';
  today = new Date();
  disabledDate = (current: Date): boolean => {
    const disabled = differenceInCalendarDays(current, this.today) > 0
      || Math.abs(differenceInCalendarDays(current, this.today)) > 59;
    return disabled;
  };
  time;
  constructor(private fcsService: FcsService,
    private msgService: NzMessageService) { }

  ngOnInit() {
    this.getLogList();
  }
  listOfSelection = [
    {
      text: '全部',
      onSelect: () => {
        this.status = '';
        this.getLogList();
      }
    },
    {
      text: '成功',
      onSelect: () => {
        this.status = 'success';
        this.getLogList();
      }
    },
    {
      text: '失败',
      onSelect: () => {
        this.status = 'failure';
        this.getLogList();
      }
    }
  ];

  timeChange() {
    if (this.time.length <= 0) {
      this.searchLogs();
    }
  }

  changeDate() {
    this.getLogList();
  }

  refresh() {
    // this.requestId = '';
    // this.time = undefined;
    // this.startTime = undefined;
    // this.endTime = undefined;
    this.searchLogs();
  }

  searchLogs() {
    this.logList = [];
    this.totalCount = 0;
    this.getLogList();
  }

  handleTime() {
    if (this.time && this.time.length > 0) {
      this.startTime = Math.floor(this.time[0].getTime() / 1000);
      this.endTime = Math.floor(this.time[1].getTime() / 1000);
    } else {
      this.startTime = null;
      this.endTime = null;
    }
  }

  searchLogList() {
    this.pageNo = 1;
    this.getLogList();
  }

  getLogList() {
    this.isLogLoading = true;
    this.handleTime();
    this.fcsService.getLogList(this.groupId, this.functionId, this.pageNo, this.pageSize, this.startTime, this.endTime, this.requestId, this.status).subscribe(res => {
      this.totalCount = res.totalCount || 0;
      if (!res.result) {
        res.result = [];
      }
      let result = res.result.map(item => {
        let type = this.invokeTypeList[item.invokeType] || item.invokeType;
        if (type === "") {
          type = "HTTP 触发器"
        }
        item.invokeTypeString = type
        // 格式化返回数据 JSON
        try {
          let returnMsg = JSON.parse(item.returnMsg)
          item.returnMsg = JSON.stringify(returnMsg, null, 4);
        } catch (error) {
          // console.log("err to parse returnMsg,", error);
        }
        // 内存转换
        item.memoryUsage = parseFloat((item.memoryUsage / 1024 / 1024).toFixed(2));
        return item
      });
      this.logList = result || [];
      if (this.requestId) {
        this.logList = result.filter(item => item.requestId === this.requestId);
      }
      if (this.totalCount > 0 && this.logList == []) {
        this.pageNo = Math.floor(this.totalCount / this.pageSize);
        this.getLogList();
      }
      this.isLogLoading = false;
    }, error => {
      this.errorMsg = error;
      this.isLogLoading = false;
    });
  }

}
